package com.wu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wu.demo.common.annotation.IgnoreAuth;
import com.wu.demo.common.annotation.RepeatSubmit;
import com.wu.demo.common.domain.AjaxResult;
import com.wu.demo.common.domain.page.TableDataInfo;
import com.wu.demo.entity.SysUser;
import com.wu.demo.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @ApiOperation(value = "用户列表listByMap")
    @PostMapping("listByMap")
    public TableDataInfo listByMap(@RequestBody SysUser user){
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("user_name", user.getUserName());
        conditions.put("sex", user.getSex());
        //SELECT * FROM sys_user WHERE user_name = ? AND sex = ?
        startPage();
        List<SysUser> userList= userService.listByMap(conditions);
        return getDataTable(userList);
    }
    @IgnoreAuth
    @ApiOperation(value = "用户列表listByQueryWrapper")
    @PostMapping("listByQueryWrapper")
    public TableDataInfo listByQueryWrapper(@RequestBody SysUser user){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","user_name");//只查询user_id,user_name字段

        //SELECT * FROM sys_user WHERE (user_name LIKE ? AND user_id > ?) ORDER BY user_id ASC LIMIT ?
        //queryWrapper.and(i -> i.like("user_name",user.getUserName()).gt("user_id",user.getUserId())).orderByDesc("user_id");
        queryWrapper.like("user_name",user.getUserName());
        queryWrapper.gt("user_id",user.getUserId());

        /*queryWrapper.like("user_name",user.getUserName()).gt("user_id",user.getUserId())
                .orderByAsc("user_id");*/
        startPage();
        List<SysUser> list = userService.list(queryWrapper);
        return getDataTable(list);
    }



    @ApiOperation(value = "用户updateWrapper")
    @PostMapping("updateWrapper")
    @RepeatSubmit  //防止重复提交
    public AjaxResult updateWrapper(){
        //把名字为ry3的用户sex更新为5，其他属性不变 UPDATE sys_user SET sex=?,nick_name=? WHERE (user_name = ?)
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name","ry3");
        SysUser user = new SysUser();
        user.setSex("5");
        user.setNickName("7777777777");
        userService.update(user,updateWrapper);



        //UPDATE sys_user SET sex=?,nick_name=? WHERE (user_name = ? and sex = ?)
        UpdateWrapper<SysUser> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("user_name","ry4").eq("sex",1).set("sex",4).set("nick_name","66666");
        userService.update(updateWrapper2);

        return AjaxResult.success();
    }

    @ApiOperation(value = "用户save")
    @PostMapping("save")
    @RepeatSubmit  //防止重复提交
    public AjaxResult save( @RequestBody @Validated SysUser user){

        userService.save(user);
        Long id = user.getUserId();
        return AjaxResult.success(id);
    }
}
